qx.Class.define("HersPOS.ItemTableModel",
{
  extend : qx.ui.table.model.Remote,

  construct : function()
  {
    this.base(arguments);
    this.row_count_url = "/HersPOS/default/total_item_rows.json";
    this.row_data_url = "/HersPOS/default/get_item_rows.json";
    this.params = "?query=";

    this.colnames = [ "id", "Product Name", "Price", "Base Price" ];

    this.colIndex = [ "id", "name", "price", "base_price" ];

    this.setColumns(this.colnames, this.colIndex);
    this.setBlockSize(1000);
  },

  members :
  {
    // overloaded - called whenever the table requests the row count
    /**
     * TODOC
     *
     * @return {void} 
     */
    _loadRowCount : function()
    {
      // Call the backend service (example) - using XmlHttp
      var req = new qx.io.remote.Request(this.row_count_url, "GET", "application/json");

      // Add listener
      req.addListener("completed", this._onRowCountCompleted, this);

      // send request
      req.send();
    },

    // Listener for request of "_loadRowCount" method
    /**
     * TODOC
     *
     * @param response {var} TODOC
     * @return {void} 
     */
    _onRowCountCompleted : function(response)
    {
      var result = response.getContent();

      if (result != null)
      {
        // Apply it to the model - the method "_onRowCountLoaded" has to be called
        this._onRowCountLoaded(result);
      }
    },

    // overloaded - called whenever the table requests new data
    /**
     * TODOC
     *
     * @param firstRow {var} TODOC
     * @param lastRow {var} TODOC
     * @return {void} 
     */
    _loadRowData : function(firstRow, lastRow)
    {
      // Call the backend service (example) - using XmlHttp
      var parameters = "&from=" + firstRow + "&to=" + lastRow;
      var url = this.row_data_url + this.params + parameters;
      var req = new qx.io.remote.Request(url, "GET", "application/json");

      // Add listener
      req.addListener("completed", this._onLoadRowDataCompleted, this);

      // send request
      req.send();
    },

    // Listener for request of "_loadRowData" method
    /**
     * TODOC
     *
     * @param response {var} TODOC
     * @return {void} 
     */
    _onLoadRowDataCompleted : function(response)
    {
      var result = response.getContent();

      if (result != null)
      {
        // Apply it to the model - the method "_onRowDataLoaded" has to be called
        this._onRowDataLoaded(result);
      }
    }
  }
});

// Using your remote model
//
// Now that you set up the remote table model the table component can use it.
//
// var remoteTableModelInstance = new myApplication.table.RemoteDataModel();
// yourTableInstance.setTableModel(remoteTableModelInstance);
//
// That's all you need to ensure your table is using your remote model.
// Sorting your data
//
// The table component offers sortable columns to let the user sort the data the way he likes. You can enable this sort ability for each column. Since you have to pull the data yourself into the table once the user changes the sorting criteria you have to update the table data. You have to enhance the _loadRowData method with this information to inform your backend how to sort the data.
//
// // "_loadRowData" with sorting support
// _loadRowData : function(firstRow, lastRow)
// {
//     // Call the backend service (example) - using XmlHttp
//     var baseUrl  = "http://localhost/services/getTableRowData.php";
//     var parameters = "?from=" + firstRow + "&to=" + lastRow;
//
//     // get the column index to sort and the order
//     var sortIndex = this.getSortColumnIndex();
//     var sortOrder =  this.isSortAscending() ? "asc" : "desc";
//
//     // setting the sort parameters - assuming the backend knows these
//     parameters += "&sortOrder=" + sortOrder + "&sortIndex=" + sortIndex;
//
//     var url = baseUrl + parameters;
//     var req = new qx.io.remote.Request(url, "GET", "application/json");
//
//     // Add listener
//     req.addListener("completed", this._onLoadRowDataCompleted, this);
//
//     // send request
//     req.send();
// }
//